#include "stack.h"

void InitStack(ST* st)
{
	assert(st);
	st->p = NULL;
	st->max = 0;
	st->size = 0;
}

void PushStack(ST* st, int n)
{
	assert(st);
	if (st->max == st->size)
	{
		st->max = st->max == 0 ? 4 : st->max * 2;
		int* cur = (int*)realloc(st->p, sizeof(int) * st->max);
		if (cur == NULL)
		{
			perror("realloc");
			exit(-1);
		}
		st->p = cur;
	}
	st->p[st->size] = n;
	st->size++;
}

void PopStack(ST* st)
{
	assert(st);
	assert(st->size);
	st->size--;
}

int SizeStack(ST* st)
{
	assert(st);
	return st->size;
}

void Destory(ST* st)
{
	assert(st);
	free(st->p);
	st->p = NULL;
	st->max = 0;
	st->size = 0;
}

bool Empty(ST* st)
{
	assert(st);
	if (st->size == 0)
	{
		return true;
	}
	return false;
}

void Print(ST* st)
{
	assert(st);
	for (int i = 0; i < st->size; i++)
	{
		printf("%d ", st->p[i]);
	}
	printf("\n");
}
